<template>
    <div>
        <div>
            <div class="box">
                <div class="search">
                    <div class="search-top">
                        <el-icon>
                            <Search />
                        </el-icon>筛选搜索
                    </div>
                    <div>
                        <el-button @click="reset">重置</el-button>
                        <el-button type="primary" @click="searchFn">查询搜索</el-button>
                    </div>
                </div>
                <div>
                    <div class="input">
                        <el-form-item label="商品名称：" label-width="150px">
                            <el-input v-model="payload.page.productName" />
                        </el-form-item>
                        <el-form-item label="推荐状态：" label-width="150px">
                            <el-select v-model="payload.page.recommendStatus" placeholder="全部">
                                <el-option label="未推荐" :value="0" />
                                <el-option label="推荐中" :value="1" />
                            </el-select>
                        </el-form-item>
                    </div>
                </div>
            </div>
            <div class="record">
                <div class="record-top">
                    <span>
                        <el-icon>
                            <Tickets />
                        </el-icon>
                    </span>
                    <span>数据列表</span>
                </div>
                <div>
                    <el-button @click="chooseProFn">选择品牌</el-button>
                </div>
            </div>
        </div>
        <el-table @selection-change="options" ref="multipleTableRef" border :data="state.list">
            <el-table-column type="selection" />
            <el-table-column label="编号" property="id" />
            <el-table-column label="商品名称" property="productName" />
            <el-table-column label="是否推荐">
                <template #default="scope">
                    <el-switch @click="changeStatus(scope.row)" v-model="scope.row.recommendStatus" :active-value="1"
                        :inactive-valu="0"></el-switch>
                </template>
            </el-table-column>

            <el-table-column label="排序" property="sort" />
            <el-table-column label="状态">
                <template #default="scope">
                    {{ scope.row.recommendStatus == 1 ? "推荐中" : "未推荐" }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button link type="primary" @click="sort(scope.row)">设置排序</el-button>
                    <el-button link type="primary" @click="delFn(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="footer">
            <el-form-item>
                <el-select placeholder="批量操作" v-model="params.recommendStatus">
                    <el-option label="设为推荐" :value="1" />
                    <el-option label="取消推荐 " :value="0" />
                    <el-option label="删除" :value="2" />
                </el-select>
            </el-form-item>
            <el-button type="primary" @click="recommendAllFn">确定</el-button>
            <ElConfigProvider :locale="zhCn">
                <div class="demo-pagination-block">
                    <el-pagination v-model:currentPage="payload.page.pageNum" v-model:page-size="payload.page.pageSize"
                        :page-sizes="[5, 10, 15]" :small="small" :disabled="disabled" :background="background"
                        layout="total, sizes, prev, pager, next, jumper" :total="state.total"
                        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
                </div>
            </ElConfigProvider>
        </div>
        <el-dialog v-model="show" title="选择商品">
            <div>
                <div class="search-input">
                    <el-input v-model="data.page.keyword" placeholder="商品名称搜索" class="input-with-select">
                        <template #append>
                            <el-button @click="ProSearchFn" :icon="Search" />
                        </template>
                    </el-input>
                </div>
                <el-table :data="data.list" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" />
                    <el-table-column label="商品名称" property="name" />
                    <el-table-column label="货号">
                        <template #default="scope">
                            {{ "NO." + scope.row.productSn }}
                        </template>
                    </el-table-column>
                    <el-table-column label="价格">
                        <template #default="scope">
                            {{ "￥" + scope.row.price }}
                        </template>
                    </el-table-column>
                </el-table>
                <div>
                    <el-pagination v-model:current-page="data.page.pageNum" @current-change="changePageNum"
                        v-model:page-size="data.page.pageSize" layout="prev, pager, next" :background="background"
                        :total="data.total" />
                </div>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="show = false">取消</el-button>
                    <el-button type="primary" @click="srue">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
        <el-dialog v-model="dialogVisible" title="设置排序" width="30%">
            <div class="sort">
                <el-form-item label="排序：" label-width="150px">
                    <el-input v-model="recommendSort.sort" />
                </el-form-item>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="sortSure">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>

    </div>

</template>
<script setup lang='ts'>
import { Search, Tickets } from "@element-plus/icons-vue";
import { user } from "@/api"
import { reactive, ref } from "vue";
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { ElMessage, ElMessageBox } from "element-plus";
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const show = ref(false)
const dialogVisible = ref(false)
const state = reactive<{ list: homeNewProductListModel[], total: number }>({
    list: [],
    total: 0
})
const payload = reactive<{ page: homeNewProductList }>({
    page: {
        productName: "",
        pageNum: 1,
        pageSize: 5,
        recommendStatus: ""
    },

})
const data = reactive<{ list: Iproduct[], page: user, total: number, params: productIdCreate[] }>({
    list: [],
    page: {
        keyword: "",
        pageNum: 1,
        pageSize: 5
    },
    total: 0,
    params: [{
        productId: 0,
        productName: "",

    }]

})
const params = reactive<{ ids: number[], recommendStatus: number | string }>({
    ids: [],
    recommendStatus: ""
})
const recommendSort = reactive<homeBrandUpdateSort>({
    id: 0,
    sort: 0
})
const del = reactive<delRecommend>({
    ids: []
})

// 提取函数服用
const getBrandList = () => {
    user.homeRecommendProductList(payload.page).then(res => {
        if (res.code == 200) {
            console.log(res.data.list);
            state.list = res.data.list
            state.total = res.data.total
        }
    })
}
getBrandList()

const handleSizeChange = (val: number) => {
    getBrandList()
}
// 改变pageNum时
const handleCurrentChange = (val: number) => {
    getBrandList()
}
// 搜索
const searchFn = () => {
    getBrandList()
}
// 重置
const reset = () => {
    payload.page.productName = ""
    payload.page.recommendStatus = ""
}

// 选择品牌 
// 提取函数
const getChooseList = () => {
    user.ProductList(data.page).then(res => {
        if (res.code == 200) {
            console.log(res.data.list);
            data.list = res.data.list
            data.total = res.data.total

        }
    })
}


const chooseProFn = () => {
    show.value = true
    getChooseList()

}

// 勾选
const handleSelectionChange = (val: IBrandList[]) => {
    console.log(val);
    data.params = []

    val.map(item => {
        data.params.push({
            productId: item.id,
            productName: item.name,
        })
    })
    console.log(params.ids);
}
const options = (val: IBrandList[]) => {
    params.ids = []
    del.ids = []
    val.map(item => {
        params.ids.push(item.id)
        del.ids.push(item.id)

    })
    console.log(params.ids);

}

// 分页
const changePageNum = () => {
    getChooseList()
}
// 选择品牌里面的搜索
const ProSearchFn = () => {
    getChooseList()
}
const srue = () => {
    ElMessageBox.confirm(
        '确定要修改吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        user.homeRecommendProductCreate(data.params).then(res => {
            if (res.code == 200) {
                show.value = false
                ElMessage({
                    message: res.message,
                    type: res.code === 200 ? "success" : "error",
                });
                getBrandList()
            }

        })
    }).catch(() => {
        ElMessage({
            message: "修改失败",
            type: "info",
        });
    })

}
// 点击是否推荐


const changeStatus = (row: IHomeBrandList) => {
    console.log(row);
    params.ids = [];
    if (row.id) {
        params.ids.push(row.id)
    }
    if (!row.recommendStatus) {
        params.recommendStatus = 0
        console.log(params.recommendStatus);
    } else {
        params.recommendStatus = row.recommendStatus
    }
    ElMessageBox.confirm(
        '确定要修改吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        user.homeRecommendProductUpdataRecommendStatus(params).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: res.message,
                    type: res.code === 200 ? "success" : "error",
                });
            }

        })
    }).catch(() => {
        ElMessage({
            message: "修改失败",
            type: "info",
        });
        row.recommendStatus = row.recommendStatus == 1 ? 0 : 1
    })

}
const recommendAllFn = () => {
    if (params.recommendStatus == 2) {
        // 删除

        ElMessageBox.confirm(
            '确定要删除吗？',
            '提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(() => {
            user.homeRecommendProductDelete(del).then(res => {
                if (res.code == 200) {
                    ElMessage({
                        message: res.message,
                        type: res.code === 200 ? "success" : "error",
                    });
                    getBrandList()
                }

            })
        }).catch(() => {
            ElMessage({
                message: "修改失败",
                type: "info",
            });

        })


    } else {
        ElMessageBox.confirm(
            '确定要修改吗？',
            '提示',
            {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then(() => {
            user.homeRecommendProductUpdataRecommendStatus(params).then(res => {
                if (res.code == 200) {
                    ElMessage({
                        message: res.message,
                        type: res.code === 200 ? "success" : "error",
                    });
                    getBrandList()
                }

            })
        }).catch(() => {
            ElMessage({
                message: "修改失败",
                type: "info",
            });

        })
    }



}

// 点击排序

const sort = (row: IHomeBrandList) => {
    dialogVisible.value = true
    if (row.id) {
        recommendSort.id = row.id
    }
    if (row.sort) {
        recommendSort.sort = row.sort
    }

}

const sortSure = () => {
    ElMessageBox.confirm(
        '确定要修改吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        user.homeRecommendProductUpdateSort(recommendSort).then(res => {
            if (res.code == 200) {
                dialogVisible.value = false
                ElMessage({
                    message: res.message,
                    type: res.code === 200 ? "success" : "error",
                });
                getBrandList()
            }

        })
    }).catch(() => {
        ElMessage({
            message: "修改失败",
            type: "info",
        });
    })
}

// 删除
const delFn = (id: number) => {
    del.ids = []
    del.ids.push(id)
    ElMessageBox.confirm(
        '确定要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        user.homeRecommendProductDelete(del).then(res => {
            if (res.code == 200) {
                ElMessage({
                    message: res.message,
                    type: res.code === 200 ? "success" : "error",
                });
                getBrandList()
            }

        })
    }).catch(() => {
        ElMessage({
            message: "修改失败",
            type: "info",
        });

    })

}





</script>
<style lang="less" scoped>
.box {
    border: 1px solid #c0c4d1;
    padding: 20px;
    color: black;

    .search {
        display: flex;
        justify-content: space-between;


    }

    .input {
        margin-top: 20px;
        // width: 350px;
        display: flex;
    }
}

.search-input {
    width: 300px
}

.sort {
    width: 300px;
}



.record {
    border: 1px solid #c0c4d1;
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    padding: 20px;
    color: black;
}

.footer {
    display: flex;
    margin-top: 20px;
}
</style>